package com.leyou.item.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.TbSpecGroup;
import com.leyou.item.entity.TbSpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecService {

    @Autowired
    private TbSpecGroupService tbSpecGroupService;
    /**
     * 根据分类id查询分组集合
     * @param cid
     * @return
     */
    public List<SpecGroupDTO> findSpecGroupListByCid(Long cid) {
//        select * from tb_spec_group where cid=?
//        构造条件
        QueryWrapper<TbSpecGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbSpecGroup::getCid,cid);

        List<TbSpecGroup> tbSpecGroupList = tbSpecGroupService.list(queryWrapper);
        if(CollectionUtils.isEmpty(tbSpecGroupList)){
            throw new LyException(ExceptionEnum.SPECGROUP_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(tbSpecGroupList,SpecGroupDTO.class);
    }

    /**
     * 新增规格组
     * @param tbSpecGroup
     * @return
     */
    public void saveSpecGroup(TbSpecGroup tbSpecGroup) {
        boolean b = tbSpecGroupService.save(tbSpecGroup);
        if(!b){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }


    @Autowired
    private TbSpecParamService tbSpecParamService;
    /**
     * 根据条件查询规格参数集合
     * @param cid
     * @param gid
     * @param searching
     * @return
     */
    public List<SpecParamDTO> findSpecParamListByCondition(Long cid, Long gid, Boolean searching) {
//      select * from tb_spec_param where cid=? and gid=? and searching=?
//        构造查询条件
        QueryWrapper<TbSpecParam> queryWrapper = new QueryWrapper<>();
        if(cid != null){
            queryWrapper.lambda().eq(TbSpecParam::getCid,cid);
        }
        if(gid != null){
            queryWrapper.lambda().eq(TbSpecParam::getGroupId,gid);
        }
        if(searching!= null){
            queryWrapper.lambda().eq(TbSpecParam::getSearching,searching);
        }
        List<TbSpecParam> tbSpecParamList = tbSpecParamService.list(queryWrapper);
        if(CollectionUtils.isEmpty(tbSpecParamList)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(tbSpecParamList,SpecParamDTO.class);
    }

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
    public void saveSpecParam(SpecParamDTO specParamDTO) {
//        类型转换
        TbSpecParam tbSpecParam = BeanHelper.copyProperties(specParamDTO, TbSpecParam.class);
//        保存
        boolean b = tbSpecParamService.save(tbSpecParam);
        if(!b){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);

        }

    }

    /**
     * 根据cid 查询 规格组 和 组内参数的集合
     * @param cid
     * @return
     */
    public List<SpecGroupDTO> findSpecGroupByCid(Long cid) {

//        根据cid查询规格组的集合
        List<SpecGroupDTO> groupDTOList = this.findSpecGroupListByCid(cid);
//        遍历组的集合，根据gid 查询组内参数
//        for (SpecGroupDTO specGroupDTO : groupDTOList) {
//            Long gid = specGroupDTO.getId();
//            List<SpecParamDTO> paramDTOList = this.findSpecParamListByCondition(null, gid, null);
//            if(CollectionUtils.isEmpty(paramDTOList)){
//                continue;
//            }
//            specGroupDTO.setParams(paramDTOList);
//        }
//        把规格参数一次性查出
        List<SpecParamDTO> paramDTOList = this.findSpecParamListByCondition(cid, null, null);
//        for (SpecGroupDTO specGroupDTO : groupDTOList) {
//            Long gid = specGroupDTO.getId();
//            List<SpecParamDTO> specParamDTOList = new ArrayList<>();
//            for (SpecParamDTO specParamDTO : paramDTOList) {
//                Long groupId = specParamDTO.getGroupId();
//                if(gid.longValue() == groupId.longValue()){
//                    specParamDTOList.add(specParamDTO);
//                }
//            }
//            if(CollectionUtils.isEmpty(specParamDTOList)){
//                continue;
//            }
//            specGroupDTO.setParams(specParamDTOList);
//        }
//        返回map接口，key - groupid val-gorupid对应的param的集合
        Map<Long, List<SpecParamDTO>> groupIdParamMap = paramDTOList.
                stream().
                collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        for (SpecGroupDTO specGroupDTO : groupDTOList) {
            Long gid = specGroupDTO.getId();
            specGroupDTO.setParams(groupIdParamMap.get(gid));
        }
        return groupDTOList;
    }
}
